﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Editform : Form
    {
        public Editform()
        {
            InitializeComponent();
        }



        public Editform(int id, string title, string author, string time)
        {
            InitializeComponent();

            this.id = id;
            textBox1.Text = title;
            textBox2.Text = time;
            textBox3.Text = author;
        }


        private int id;
        private void button2_Click(object sender, EventArgs e)
        {
            var titleName = textBox1.Text;
            var author = textBox2.Text;
            var createdTime = textBox3.Text;

            if (id > 0)
            {
                if (!string.IsNullOrEmpty(titleName))
                {
                    var sql = string.Format("update Articles set Title='{0}',Author='{1}',CreatedTime='{2}' where Id='{3}'  ", titleName,createdTime, author, id);
                    var res = DebHelper.AddOrUpDateOrDelete(sql);
                    if (res == 1)
                    {
                        MessageBox.Show("修改成功", "提示");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("请输入修改的数据", "提示");
                    }

                }



            }
            else
            {
                if(!string.IsNullOrEmpty(titleName))
                {
                    var sql = string.Format("insert into Articles(Title,Author,CreatedTime)values('{0}','{1}','{2}')",titleName, createdTime, author);
                    var res = DebHelper.AddOrUpDateOrDelete(sql);
                    if (res == 1)
                    {
                        MessageBox.Show("添加成功", "提示");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("添加失败", "提示");
                        this.Close();
                    }
                }
                else
                {
                    MessageBox.Show("请输入要添加的数据");
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
